<?php
/**
 * 角色管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author cmpan
 * @copyright   Copyright (c) 2009-2011 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */

class RoleController extends UE_Controller_Admin {
	/**
	 * 角色实例
	 *
	 * @var RoleModel
	 */
	public $role;
	function init() {
		UE_Loader::loadModel('RoleModel', $this->app);
		$this->role = new RoleModel();
	}
	
	public function listAction() {
		$type = empty($_GET['role_type']) ? '' : $_GET['role_type'];
		
		if (IS_POST) {
			if(!empty($_POST['optype']) && in_array($_POST['optype'], array('sort', 'enable', 'disable', 'del'))) {
				switch ($_POST['optype']) {
					case 'sort':
						foreach ($_POST['sortItem'] as $key => $val) {
							$this->role->setId($key)->update(array('role_order'=>$val));					
						}
						break;
					case 'enable':
						foreach ($_POST['itemcheck'] as $key => $val) {
							$this->role->setId($key)->update(array('role_disabled'=>'0'));					
						}
						break;
					case 'del':
						foreach ($_POST['itemcheck'] as $key => $val) {
							$this->role->setId($key)->delete();					
						}
						break;
					case 'disable':
						foreach ($_POST['itemcheck'] as $key => $val) {
							$this->role->setId($key)->update(array('role_disabled'=>'1'));					
						}
						break;
				}
			}
		}
		
		
	    $roles = $this->role->getRoles($type);
	    
	    include $this->tpl();
	}
	
	public function addAction() {
		if (IS_POST) {			
			$this->role = new RoleModel();
			if(!$this->role->add($_POST)) {
				$this->setErr($this->role->getErrs());
			} else {
				$this->setOK(UE_Lang::get('user.role.add.ok'));
			}
		}
		
	    include $this->tpl();
	}	
	
	/**
	 * 编辑角色
	 *
	 */
	public function editAction() {
		if (empty($this->id)) {
	    	print 'Error Param';
	    	return ;
	    }
	    
	    if (IS_POST) {
	    	$_POST['role_name'] = trim($_POST['role_name']);
		
			if (empty($_POST['role_name']) || empty($_POST['role_type'])) {
				$this->setErr(UE_Lang::get('missField'));				
			} else {
				if($this->role->setId($this->id)->update($_POST)){
					$this->setOK('成功修改角色');
				} else {
					$this->setErr($this->role->getErrs());
				}
				//print_r($_POST);
			}
	    }
	    
	    $role = $this->role->read($this->id);
	    
	    
	    include $this->tpl();
	}
	
	/**
	 * 删除角色
	 *
	 */
	public function delAction() {
	    if (empty($this->id)) {
	    	print 'Error Param';
	    	return ;
	    }
	    
	    $this->role->setId($this->id)->delete();
	    header('Location:'.UE::referer());
	}
}

